package com.bw.counter.view

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.PopupMenu
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.common.view.BaseMvvmActivity
import com.bw.counter.R
import com.bw.counter.adapter.CounterAdapter
import com.bw.counter.databinding.ActivityCounterBinding
import com.bw.counter.entity.CounterData
import com.bw.counter.net.CounterViewModel
/**
 *  智慧园区-05-收银台 -v1.1-张梦停
 *  2025/05/09
 */
@Route(path = RoutePath.PATH_COUNTER)
class CounterActivity : BaseMvvmActivity<ActivityCounterBinding,CounterViewModel>() {
    lateinit var counterAdapter: CounterAdapter
    private val originNewList = mutableListOf<CounterData>() // 原始数据
    override fun initView() {
        binding.counterTop.tvTitle.text = "收银台"
        binding.counterTop.ivBack.setOnClickListener { finish() }
        binding.counterTop.ivMore.visibility = View.VISIBLE
        binding.counterTop.ivMore.setOnClickListener {
            val menu11 = PopupMenu(this, it)
            menu11.menuInflater.inflate(R.menu.item_menu,menu11.menu)
            menu11.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.add_menu->{
                        ARouter.getInstance().build(RoutePath.PATH_ADD_Count)
                            .navigation()
                        true
                    }
                    R.id.code->{
                        ARouter.getInstance().build(RoutePath.PATH_Count_Code_Detail).navigation()
                        true
                    }
                    else->false
                }
                true
            }
            menu11.show()

        }
        counterAdapter = CounterAdapter()
        binding.re.adapter = counterAdapter
        binding.re.layoutManager = LinearLayoutManager(this)
        counterAdapter.setOnItemClickListener { adapter, view, position ->
            val counterData = counterAdapter.data[position]
            ARouter.getInstance().build(RoutePath.PATH_Count_DETAIL)
                .withSerializable("detail",counterData)
                .navigation()
        }
        counterAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("删除当前订单")
                .setNegativeButton("取消",null)
                .setPositiveButton("确定",DialogInterface.OnClickListener { dialogInterface, i ->
                    vm.delItem(counterAdapter.data[position].id)
                    counterAdapter.removeAt(position)
                    counterAdapter.notifyItemRemoved(position)
                })
                .show()
            true
        }
        binding.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
            override fun afterTextChanged(s: Editable?) {
                val query = s.toString()
                val filteredList = if (query.isEmpty()) {
                    originNewList
                } else {
                    originNewList.filter {
                        it.payerName.contains(query, ignoreCase = true)||
                                it.contactNumber.contains(query, ignoreCase = true)
                    }
                }
                counterAdapter.data.clear()
                counterAdapter.data.addAll(filteredList)
                counterAdapter.notifyDataSetChanged()
            }
        })
    }

    override fun initData() {
        vm.getCounterSuccess.observe(this){
            originNewList.clear()
            originNewList.addAll(it)
            counterAdapter.data.addAll(originNewList)
            counterAdapter.data.reverse()
            counterAdapter.notifyDataSetChanged()
        }
        vm.deleSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        counterAdapter.data.clear()
        vm.getCount(1)
    }
}